package com.itheima.api.suanfa;

public class erfenfa {
    public static void main(String[] args) {
        int arr[]={1,2,3,4,5,6,7,8,9};
        int num=11;
      int index=erfenFindNum(arr,num);
      if (index==-1){
          System.out.println("该数组中没有此数");
      }else {
          System.out.println(num+"在"+index+"索引位置");
      }

    }

    private static int erfenFindNum(int[] arr, int num) {
        int start=0;
        int end=arr.length-1;
        while (start<=end){
            int mid=(start+end)/2;
            if (arr[mid]>num){
            end=mid-1;
        }else if (arr[mid]<num){
            start=mid+1;
        }else{
            return mid;
        }
    }
        return -1;
    }
}
